package org.jeecg.modules.htxincailiao.news.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.news.entity.HTNews;
import org.jeecg.modules.htxincailiao.news.mapper.HTNewsMapper;
import org.jeecg.modules.htxincailiao.news.service.IHTNewsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

import java.util.Map;
import java.util.Objects;

import static org.jeecg.common.contant.HTAuditStatusEnum.NOT_AUDIT;
import static org.jeecg.common.contant.HTAuditStatusEnum.PASSED;
import static org.jeecg.common.contant.HTHomeRecommendedEnum.NOT_RECOMMENDED;
import static org.jeecg.common.contant.HTHomeRecommendedEnum.RECOMMENDED;
import static org.jeecg.modules.htxincailiao.news.constant.HTNewsSourceTypeEnum.REPRINTED;

/**
 * @Description: ht_news
 * @Author: jeecg-boot
 * @Date:   2021-08-11
 * @Version: V1.0
 */
@Service
public class HTNewsServiceImpl extends ServiceImpl<HTNewsMapper, HTNews> implements IHTNewsService {

    @Override
    public boolean saveForCrawlerCollectBy(String categoryId,
                                           String name,
                                           String subname,
                                           String source,
                                           String cover,
                                           String targetSite,
                                           String content) {
        if (oConvertUtils.isEmpty(name)) {
            return true;
        }

        HTNews storedNews = getByName(name);
        if(oConvertUtils.isEmpty(storedNews)){
            HTNews news = new HTNews();
            news.setCategoryId(categoryId);
            news.setName(name);
            news.setSubname(subname);
            news.setSourceType(REPRINTED.getValue());
            news.setSource(source);
            news.setSite(targetSite);
            news.setCover(cover);
            news.setContent(content);
            news.setIsHomeRecommended(RECOMMENDED.getValue());
            news.setAuditStatus(PASSED.getValue());
            news.setAuditStatus(2);//爬取的新闻默认审核通过
            return save(news);
        }
        else {
            return true;
        }

    }

    @Override
    public HTNews getByName(String name) {
        return baseMapper.getByName(name);
    }
}
